package binsearchTree;

/**
 * Created with IntelliJ IDEA.
 * Description：
 * User：user
 * DATE:2021-11-14
 * Time:10:00
 */
class BinarySearchTree{
    static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int val){
            this.val = val;
        }
    }

    public TreeNode root;

    //最坏情况下查找的时间复杂度为O(log2N)
    public TreeNode findVal(int key){
        TreeNode cur = root;
        if(root == null)return null;
        while (cur != null){
            if(cur.val < key){
                cur = cur.right;
            }else if(cur.val == key){
                return  cur;
            }else{
                cur = cur.left;
            }
        }
        return  null;
    }

    //插入
    public  void  insert(int val){
        TreeNode treeNode = new TreeNode(val);
        if(root == null)root = treeNode;
        TreeNode parent = null;
        TreeNode cur = root;
        while (cur != null){
            if(cur.val < val){
                parent = cur;
                cur = cur.right;
            }else if(cur.val == val){//结点已经存在无法插入
                return;
            }else{
                parent = cur;
                cur = cur.left;
            }
        }
        if(parent.val > val){ //判断插入左树还是插入右数
            parent.left =treeNode;
        }else{
            parent.right = treeNode;
        }
    }


    //删除结点三种情况  前提是有没有这个结点,找到这个结点
    //找到这个结点之后删除这个结点 记录这个结点的前驱结点
    public void remove(int key){
        if(root == null)return;
        TreeNode parent = null;
        TreeNode cur = root;
        while (cur != null){
            if(cur.val < key){
                parent = cur;
                cur = cur.right;
            }else if(cur.val == key){
                removeNode(parent,cur);
            }else{
                parent = cur;
                cur = cur.left;
            }
        }
    }
    //左树为空:cur = root cur = parent.left cur = parent.right
    //右树为空:cur = root cur = parent.left cur = parent.right
    //左右树都不为空:替罪羊法  当前结点的左树的右树中找到最大值
    //                     当前结点的右树的左树中找到左小值
    //parent代表要删除结点的父亲节点
    //cur代表要删除的结点
    private void removeNode(TreeNode parent,TreeNode cur){
        if(cur.left == null){
            if(cur == root){
                root = cur.right;
            }else if(cur == parent.left){
                parent.left = cur.right;
            }else{
                parent.right = cur.right
            }
        }else if(cur.right == null){
            if(cur == root){
                root = root.left;
            }else if(cur == parent.left){
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }else{
            TreeNode tp = cur;
            TreeNode tar = cur.right;
            while (tar.left != null){
                tp = tar;
                tar = tar.left;
            }
            cur.val = tar.val;
            if(tar == tp.left){
                tp.left = tar.right
            }else{
                tp.right = tar.right;
            }
        }
    }

}
public class Test {
    public static void main(String[] args) {
        int[] array = {12,3,6,10,8};
        BinarySearchTree bina = new BinarySearchTree();
        for (int x :array) {
            bina.insert(x);
        }
        System.out.println("aaa");
    }
}
